package com.xch.wx.controller;

import com.xch.bean.BootstrapTableExpress;
import com.xch.bean.Express;
import com.xch.bean.Message;
import com.xch.bean.User;
import com.xch.mvc.ResponseBody;
import com.xch.mvc.ResponseView;
import com.xch.service.ExpressService;
import com.xch.util.DateFormatUtil;
import com.xch.util.JSONUtil;
import com.xch.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class QRCodeController {

    @ResponseView("/wx/createQRCode.do")
    public String createQRCode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        String type = request.getParameter("type");
        String userPhone = null;
        String qRCodeContent  = "";
        if("express".equals(type)){
            //快递二维码，单个快递
            qRCodeContent = "express_"+code;
        }else{
            //用户二维码，所有快递
            User wxUser = UserUtil.getWxUser(request.getSession());
            userPhone = wxUser.getUserPhone();
            qRCodeContent = "user_"+userPhone;
        }
        HttpSession session = request.getSession();
        session.setAttribute("qrcode",qRCodeContent);
        return "/personQRcode.html";
    }

    @ResponseBody("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始产生二维码");
        HttpSession session = request.getSession();
        String qrcode = (String)session.getAttribute("qrcode");
        Message msg = new Message();
        if(qrcode == null){
            msg.setStatus(-1);
            msg.setResult("取件码获取出错，请重新操作");
        }else{
            msg.setStatus(0);
            msg.setResult(qrcode);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest request,HttpServletResponse response){
        String code = request.getParameter("code");
        boolean b = ExpressService.updateStatus(code);
        Message msg = new Message();
        if(b){
            msg.setStatus(0);
            msg.setResult("取件成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("取件失败");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/findExpressByCode")
    public String findExpressByCode(HttpServletRequest request,HttpServletResponse response){
        String code = request.getParameter("code");
        Express e = ExpressService.findByCode(code);
        BootstrapTableExpress e2 = null;
        if(e != null){
            e2 = new BootstrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),DateFormatUtil.format(e.getInTime()),e.getCompany(),e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime()),e.getCode(),e.getStatus()==0?"待取件":"已取件",e.getSysPhone());

        }
        Message msg = new Message();
        if(e2 == null){
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e2);
        }
        return JSONUtil.toJSON(msg);
    }
}
